package cn.b.sky.shop.activiti.controller;

import cn.b.sky.shop.activiti.model.SkyService;
import cn.b.sky.shop.activiti.model.ProcessModelCategory;
import cn.b.sky.shop.activiti.model.ServiceTest;
import cn.b.sky.shop.activiti.model.condition.SkyServiceCondition;
import cn.b.sky.shop.activiti.service.SkyServiceService;
import cn.b.sky.shop.activiti.service.ProcessModelCategoryService;
import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.exception.ImeException;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;
import cn.b.sky.user.enums.ECodeEnums;
import cn.b.sky.user.service.ECodeService;
import cn.b.sky.user.service.model.ECode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <strong>Title : ServiceController.java<br>
 * </strong> <strong>Package : cn.b.sky.shop.activiti.controller<br>
 * </strong> <strong>Description : </strong>ime服务的控制器，服务跳转，服务相关操作<br>
 * <strong>Create on : 2015-7-2 上午10:50:54<br>
 * </strong>
 * <p>
 * 
 * @author lidapeng<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 *          <strong>修改历史:</strong><br>
 *          修改人 | 修改日期 | 修改描述<br>
 *          -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping(value = "/service/*")
public class ServiceController extends SecurityController {

  @Resource
  private SkyServiceService SkyService;
  
  @Resource
  ECodeService eCodeService;

  @Resource
  private ProcessModelCategoryService processModelCategoryService;

  @RequestMapping("serviceList")
  public String serviceList(Model model, @ModelAttribute("condition") SkyServiceCondition service, String navTabId) {
    List<ECode> ecode = eCodeService.selectECodeList(ECodeEnums.SERVICE_TYPE.getCode(),SecurityContextUtil.getLanguageCode());
    List<SkyService> services = SkyService.getService(service);
    int count = SkyService.getServiceCount(service);
    service.setData(services);
    service.setTotalCount(count);
    model.addAttribute("ecodes", ecode);
    model.addAttribute("page", service);
    model.addAttribute(NAVTABID, navTabId);
    return "service/service_list";
  }

  @RequestMapping("addService")
  public String addService(Model model, String navTabId) {
    List<ECode> ecode = eCodeService.selectECodeList(ECodeEnums.SERVICE_TYPE.getCode(),SecurityContextUtil.getLanguageCode());
    model.addAttribute("ecodes", ecode);
    model.addAttribute(NAVTABID, navTabId);
    return "service/service_add";
  }

  @RequestMapping("viewService")
  public String viewService(Model model, String navTabId, Integer id) {
    SkyService service = SkyService.getImeServiceById(id);
    List<ECode> ecode = eCodeService.selectECodeList(ECodeEnums.SERVICE_TYPE.getCode(),SecurityContextUtil.getLanguageCode());
    model.addAttribute("ecodes", ecode);
    model.addAttribute("service", service);
    return "service/service_view";
  }

  @RequestMapping("modifyService")
  public String modifyService(Model model, String navTabId, Integer id) {
    SkyService service = SkyService.getImeServiceById(id);
    List<ECode> ecode = eCodeService.selectECodeList(ECodeEnums.SERVICE_TYPE.getCode(),SecurityContextUtil.getLanguageCode());
    model.addAttribute("ecodes", ecode);
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("service", service);
    return "service/service_add";
  }

  @RequestMapping("viewProcess")
  public String viewProcess(Model model, String navTabId, Integer id) {
    SkyService service = SkyService.getImeServiceAndProcessByServiceId(id);
    List<ECode> ecode = eCodeService.selectECodeList(ECodeEnums.SERVICE_TYPE.getCode(),SecurityContextUtil.getLanguageCode());
    model.addAttribute("ecodes", ecode);
    model.addAttribute("service", service);
    return "service/process_list";
  }

  @RequestMapping("testService")
  public String testService(Model model, String navTabId, Integer id) {
    SkyService service = SkyService.getImeServiceById(id);
    model.addAttribute("service", service);
    return "service/service_test";
  }

  @RequestMapping("runService")
  @ResponseBody
  public DWZResult runService(Model model, Integer id, String inParam) {
    SkyService service = SkyService.getImeServiceById(id);
    service.setInParam(inParam);
    ServiceTest serviceTest = SkyService.runImeServie(service);
    return ajaxDone(serviceTest);
  }

  @RequestMapping("saveOrUpdateService")
  @ResponseBody
  public DWZResult saveImeService(Model model, String navTabId, SkyService service, HttpServletRequest request) {
    if (service == null) {
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), SystemConstant.PARAMETERNULL));
    }

    setCreateInfo(service);
    if (service.getId() == null) {
      SkyService.saveImeService(service);
      String forwardUrl = getForwardUrl(request) + "/service/serviceList?navTabId=" + navTabId;
      return dialogAjaxDone(navTabId, null, forwardUrl, null);
    } else {
      // 修改不关闭页面，也不刷新父页面
      SkyService.modifyImeService(service);
      return dialogAjaxDone(null, null, null, null);
    }

  }

  /**
   * 模型树
   */
  @RequestMapping("modelServices")
  public String toParamList(Model model, String navTabId) {
    List<ProcessModelCategory> pmc = processModelCategoryService.getProcessModelCategoryTree(this.getSiteCode());
    model.addAttribute("processModel", pmc);
    model.addAttribute(NAVTABID, navTabId);
    return "service/process_services";
  }

  @RequestMapping("getProcessServiceList")
  public String getProcessServiceList(Model model, String navTabId, String modelId) {
    if (StringUtils.isBlank(modelId)) {
      throw ImeException.newException(SystemConstant.PARAMETERNULL);
    }
    List<SkyService> services = SkyService.searchServiceByModelId(modelId);
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("services", services);
    model.addAttribute("modelId", modelId);
    return "service/serviceDetail";
  }

  /**
   * 
   * <strong>Title : servicesLookup<br></strong>
   * <strong>Description : </strong>给流程分配服务<br> 
   * <strong>Create on : 2015-8-4 下午4:06:20<br></strong>
   * <p>
   * @param model
   * @param navTabId
   * @param condition
   * @param modelId
   * @return
   * String
   * @throws 
   * @author lidapeng<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping("servicesLookup")
  public String servicesLookup(Model model, String navTabId,
                               @ModelAttribute("condition") SkyServiceCondition condition, String modelId) {
    condition.setStatus("0");//启用
    condition.setModelId(modelId);
    List<SkyService> services = SkyService.getService(condition);
    int count = SkyService.getServiceCount(condition);
    condition.setData(services);
    condition.setTotalCount(count);
    model.addAttribute("page", condition);
    model.addAttribute("modelId", modelId);
    model.addAttribute(NAVTABID, navTabId);
    List<ECode> ecode = eCodeService.selectECodeList(ECodeEnums.SERVICE_TYPE.getCode(),SecurityContextUtil.getLanguageCode());
    model.addAttribute("ecodes", ecode);
    return "service/services_lookup";
  }

  @RequestMapping("grantServicesToProcess")
  @ResponseBody
  public DWZResult grantServicesToProcess(Model model, String serviceId, String modelId, String flag) {
    if (StringUtils.isBlank(serviceId) || StringUtils.isBlank(modelId)) {

      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), SystemConstant.PARAMETERNULL));
    }
    if ("add".equals(flag)) {
      SkyService.saveProcessService(modelId, serviceId);
    } else if ("delete".equals(flag)) {
      SkyService.deleteProcessService(modelId, serviceId);
    }
    return ajaxDone(null);
  }

}
